<?php
/*
 * Copyright (c) 2022.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\controller\adminapi\platform\app;

use app\controller\adminapi\platform\PlatformController;
use app\common\model\addon\Addons;
use app\common\model\platform\PlatformApp;
use app\common\model\platform\PlatformSetting;
use hiduoke\services\CloudServer;
use think\App;

/**
 * 提交审核
 */
class Upload extends PlatformController
{
    /**
     * @var PlatformApp
     */
    public $model;

    public function __construct(App $app, PlatformApp $model)
    {
        parent::__construct($app);
        $this->model = $model;
        $this->cloud = new CloudServer();
    }

    /**
     * 获取小程序更新信息
     * @return \think\response\Json
     */
    public function getAddonAppUpdateMsg()
    {
        $app_id = $this->request->post('app_id', 0);
        $addon_name = $this->request->post('addon_name', '');
        $addon_app = (new PlatformApp())->where(['id' => $app_id])->append(['addon'])->find();
        $addon_app['name'] = $addon_app['name'] ?: $addon_app['addon']['title'];
        $addon_app['logo'] = $addon_app['logo'] ?: $addon_app['addon']['icon'];
        if (!$addon_app) {
            return $this->error('小程序错误');
        }
        $res = $this->cloud->getAddonAppUpdateMsg($addon_name);
        if ($res['code'] == 0) {
            $data = $res['data'];
            if ($res['data']['version'] == $addon_app['version']) {
                return $this->success('no_new_version', ['cloud' => $data, 'local' => $addon_app]);
            } else {
                return $this->success('', ['cloud' => $data, 'local' => $addon_app]);
            }
        } else {
            return $this->error($res['msg']);
        }
    }

    /*
     * 更新小程序
     */
    public function updateAddonApp()
    {
        $addon_name = $this->request->post('addon_name', '');
        $changeLog = input('changeLog', '');
        $version = input('version', '');
        $platform_appid = input('platform_appid', 0);
        $addon = (new Addons())->where('name', $addon_name)->find();
        if (!$addon) {
            return $this->error('应用不存在');
        }
        $addon_app = (new PlatformApp())->where(['id' => $platform_appid])->find();
        $config = (new PlatformSetting())->getValueByKey('upload', $addon_app['plaid'], $addon_app['id']);
        $domain = $config['domain'] ?? '';
        $domain = $domain ?: $this->request->domain();
        if (strpos($domain, 'https://') === false) {
            return $this->error('当前提交地址非Https,请改为https后提交', ['error' => 1]);
        }
        if (!$addon_app) {
            return $this->error("小程序不存在");
        }
        $app = $this->cloud->getAddonApp($addon_name);
        // 获取小程序信息
        if ($app['code'] == 0) {
//            try {
                $setting = (new PlatformSetting())->getValueByKey('upload', get_plaid(), $platform_appid);
                $appMsg = $app['data'];
                if ($addon['app_type'] == 'dy') {
                    $data = [
                        'down_url'       => $appMsg['new_app_version_file'],
                        'addon_name'     => $addon_name,
                        'version'        => $version ?: $appMsg['new_app_version'],
                        'email'          => $setting['email'],
                        'password'       => $setting['password'],
                        'token'          => $setting['token'],
                        'appid'          => $setting['appid'],
                        'changeLog'      => $changeLog,
                        'platform_appid' => $platform_appid,
                        'plaid'          => get_plaid(),
                        'baseUrl'        => $domain,
                    ];
                } else if ($addon['app_type'] == 'wx') {
                    $data = [
                        'down_url'          => $appMsg['new_app_version_file'],
                        'addon_name'        => $addon_name,
                        'version'           => $version ?: $appMsg['new_app_version'],
                        'appid'             => $setting['appid'],
                        'changeLog'         => $changeLog,
                        'platform_appid'    => $platform_appid,
                        'plaid'             => get_plaid(),
                        'baseUrl'           => $domain,
                        'privateKeyContent' => $setting['privateKey'],
                        'robot'             => 1
                    ];
                }
                $res = $this->cloud->updateAddonApp($data, $addon['app_type']);
                if ($res['code'] == 0) {
                    $addon_app->version = $data['version'];
                    $addon_app->save();
                    return $this->success('ok', $res['data']);
                } else {
                    return $this->error($res['msg']);
                }
//            } catch (\Exception $e) {
//                return $this->error("上传设置未配置或者配置错误1".$e->getMessage() );
//            }
        } else {
            return $this->error($app['msg']);
        }
    }

}
